#include "../include/workerManager.h"
#include "../include/boss.h"
#include "../include/employee.h"
#include "../include/manager.h"
#include "../include/mysql/mysql.h"
#include <cstdio>
#include <string.h>

WorkerManager::WorkerManager() {}

WorkerManager::~WorkerManager() {}

// 展示菜单
void WorkerManager::showMenu() {
  cout << "********************************************" << endl;
  cout << "*********  欢迎使用职工管理系统！ **********" << endl;
  cout << "*************  1.增加职工信息  *************" << endl;
  cout << "*************  2.显示职工信息  *************" << endl;
  cout << "*************  3.删除离职职工  *************" << endl;
  cout << "*************  4.修改职工信息  *************" << endl;
  cout << "*************  5.查找职工信息  *************" << endl;
  cout << "*************  6.按照编号排序  *************" << endl;
  cout << "*************  7.清空所有信息  *************" << endl;
  cout << "*************  0.退出管理程序  *************" << endl;
  cout << "********************************************" << endl;
  cout << endl;
}

// 退出系统
void WorkerManager::exitSystem() {
  cout << "欢迎您下次光临！" << endl;

  pause();
  exit(0);
}

// 增加职工
int WorkerManager::addWorker(MysqlManagement MM) {
  cout << "请输入新员工的数量：";
  int countNewWorker;
  cin >> countNewWorker;

  // 判断输入是否有效
  if (cin.fail()) {
    cin.sync();
    cin.clear();

    cout << "输入非法字符！" << endl;
    return 0;
  }

  int i = 1;
  while (i <= countNewWorker) {
    Worker *newWorker = (Worker *)malloc(sizeof(Worker));
    cout << "请输入第" << i << "个员工的编号：";
    cin >> newWorker->w_ID;
    cout << "请输入第" << i << "个员工的姓名：";
    cin >> newWorker->w_Name;
    cout << "1.老板\n2.经理\n3.职工\n";
    cout << "请选择第" << i << "个员工的岗位：";
    cin >> newWorker->w_DeptId;
    if (cin.fail()) {
      cin.sync();
      cin.clear();

      cout << "输入非法字符！" << endl;
      return 0;
    }

    int ret;
    switch (newWorker->w_DeptId) {
    case 1:
    case 2:
    case 3:
      ret = MM.addDB(newWorker);
      break;
    default:
      cout << "岗位编号输入错误！" << endl;
      ret = 0;
      break;
    }

    if (ret == 1) {
      cout << "第" << i << "个员工添加成功！" << endl;
      i++;
    } else {
      cout << "第" << i << "个员工添加失败！" << endl;
    }

    Worker *temp = newWorker;
    free(temp);
    cout << endl;
  }

  return 0;
}

// 显示员工
int WorkerManager::showWorker(MysqlManagement MM) {
  char sql[1024];
  sprintf(sql, "select * from worker;");
  int qret = mysql_query(MM.db, sql);
  if (qret != 0) {
    printf("Print,error :%s\n", mysql_error(MM.db));
    return -1;
  }
  MYSQL_RES *result = mysql_store_result(MM.db);
  cout << "一共有" << mysql_num_rows(result) << "个员工。" << endl;

  MM.showDB(result);

  return 0;
}

int WorkerManager::deleteWorker(MysqlManagement MM) {
  cout << "请输入您想删除的职工号：";
  int number;
  cin >> number;
  if (cin.fail()) {
    cin.sync();
    cin.clear();

    cout << "输入非法字符！" << endl;
    return 0;
  }

  int ret = MM.deleteDB(number);
  if (ret == 0) {
    return 0;
  } else {
    return 1;
  }
}

// 显示职工信息
int WorkerManager::searchWorker(MysqlManagement MM) {
  cout << "请输入您想查找的职工号：";
  int number;
  cin >> number;
  if (cin.fail()) {
    cin.sync();
    cin.clear();

    cout << "输入非法字符！" << endl;
    return 0;
  }

  if (!MM.isInDB(number)) {
    cout << "无该职工号！请重新输入！" << endl;
    return 0;
  }

  char sql[1024];
  sprintf(sql, "select * from worker where id = '%d';", number);
  int qret = mysql_query(MM.db, sql);
  if (qret != 0) {
    printf("Print,error :%s\n", mysql_error(MM.db));
    return -1;
  }
  MYSQL_RES *result = mysql_store_result(MM.db);

  int ret = MM.showDB(result);
  if (ret == 1)
    return 1;
  else
    return 0;
}

// 修改职工信息
int WorkerManager::alterWorker(MysqlManagement MM) {
  cout << "请输入您想修改的职工号：";
  int number;
  cin >> number;
  if (cin.fail()) {
    cin.sync();
    cin.clear();

    cout << "输入非法字符！" << endl;
    return 0;
  }

  if (!MM.isInDB(number)) {
    cout << "无该职工号！请重新输入！" << endl;
    return 0;
  }

  char sql[1024];
  sprintf(sql, "select * from worker where id = '%d';", number);
  int qret = mysql_query(MM.db, sql);
  if (qret != 0) {
    printf("Print,error :%s\n", mysql_error(MM.db));
    return -1;
  }
  MYSQL_RES *result = mysql_store_result(MM.db);

  // 第一次展示
  MM.showDB(result);

  // 修改值

  cout << "请输入您修改的内容！" << endl;
  cout << "新的编号：";
  int newId;
  cin >> newId;

  cout << "新的名字：";
  char name[20];
  cin >> name;

  cout << "新的岗位：";
  int deptId;
  cin >> deptId;
  if (cin.fail()) {
    cin.sync();
    cin.clear();

    cout << "输入非法字符！" << endl;
    return 0;
  }

  Worker *worker = NULL;
  cout << endl;
  if (deptId == 1) {
    worker = new Boss(newId, name, deptId);
    worker->showInfo();
  } else if (deptId == 2) {
    worker = new Manager(newId, name, deptId);
    worker->showInfo();
  } else if (deptId == 3) {
    worker = new Employee(newId, name, deptId);
    worker->showInfo();
  } else {
    cout << "非法字符！" << endl;
  }

  int ret = MM.alterDB(number, worker);

  delete worker;

  if (ret == 1)
    return 1;
  else
    return 0;
}

// 清空所有信息
//
int WorkerManager::clearWorker(MysqlManagement MM) {
  cout << "确认您是否真的要清空数据？(y or n)：";
  char select1;
  cin >> select1;

  if (select1 == 'n') {
    cout << "停止清空数据！" << endl;
    return 0;
  }

  cout << "再次确认您是否真的要清空数据？(y or n)：";
  char select2;
  cin >> select2;

  if (select1 == 'n') {
    cout << "停止清空数据！" << endl;
    return 0;
  }

  MM.clearDB();

  getchar();
  return 1;
}

// 排序员工
int WorkerManager::sortWorker(MysqlManagement MM) {
  cout << "1.按照员工id升序排列" << endl;
  cout << "2.按照员工id降序排列" << endl;
  cout << "3.退出";
  cout << "你的选择是：";

  int select;
  cin >> select;

  char sql[1024];

  switch (select) {
  case 1: {
    sprintf(sql, "select * from worker order by id asc;");
    int qret = mysql_query(MM.db, sql);
    if (qret != 0) {
      cout << "排序失败！" << endl;
    }
    MYSQL_RES *result = mysql_store_result(MM.db);
    MM.showDB(result);
    break;
  }
  case 2: {
    sprintf(sql, "select * from worker order by id desc;");
    int qret = mysql_query(MM.db, sql);
    if (qret != 0) {
      cout << "排序失败！" << endl;
    }
    MYSQL_RES *result = mysql_store_result(MM.db);

    MM.showDB(result);
    break;
  }
  case 0:
    cout << "退出成功！请按空格键继续..." << endl;
    getchar();
    getchar();
    break;
  default:
    cout << "输入非法字符！请按空格键继续..." << endl;
    getchar();
    getchar();
    break;
  }

  return 0;
}
